package com.bootdo.crm.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.crm.domain.AchievementDO;
import com.bootdo.crm.service.AchievementService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 业绩目标
 * 
 * @author cui
 * @email 1992lcg@163.com
 * @date 2020-02-20 13:14:27
 */
 
@Controller
@RequestMapping("/crm/achievement")
public class AchievementController {
	@Autowired
	private AchievementService achievementService;
	
	@GetMapping()
	@RequiresPermissions("crm:achievement:achievement")
	String Achievement(){
	    return "crm/achievement/achievement";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("crm:achievement:achievement")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<AchievementDO> achievementList = achievementService.list(query);
		int total = achievementService.count(query);
		PageUtils pageUtils = new PageUtils(achievementList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("crm:achievement:add")
	String add(){
	    return "crm/achievement/add";
	}

	@GetMapping("/edit/{achievementId}")
	@RequiresPermissions("crm:achievement:edit")
	String edit(@PathVariable("achievementId") Integer achievementId,Model model){
		AchievementDO achievement = achievementService.get(achievementId);
		model.addAttribute("achievement", achievement);
	    return "crm/achievement/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("crm:achievement:add")
	public R save( AchievementDO achievement){
		if(achievementService.save(achievement)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("crm:achievement:edit")
	public R update( AchievementDO achievement){
		achievementService.update(achievement);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("crm:achievement:remove")
	public R remove( Integer achievementId){
		if(achievementService.remove(achievementId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("crm:achievement:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] achievementIds){
		achievementService.batchRemove(achievementIds);
		return R.ok();
	}
	
}
